<?php

/**
 * Class for viewing a select element.
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class ViewRadio extends ViewHtmlElement {

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {

        $fields["tag"] = "div";
        $fields["data"] = isset($fields["data"]) ? $fields["data"] : array();

        parent::__construct($fields);
        
        /**
         * Loop over the possible values, creating the individual radio buttons.
         */
        foreach ($this->getData() as $key => $datum) {
            $radioValue = is_array($datum) ? $datum[0] : $datum;
            $radio = new ViewHtmlFormElement(array("tag" => "input"));
            $radio->setAttribute("value", $radioValue);
            $radio->setAttribute("id", $key + 1);
            $radio->setId($key + 1);
            $radio->setLabel(is_array($datum) ? $datum[1] : $datum);
            $radio->setAttribute("type", "radio");
            $radio->setAttribute("name", $name);
            if ($this->value == $radioValue) {
                $radio->setAttribute("checked", "checked");
            }
            $this->addInnerHtml($radio);
        }
    }

}